package com.carrentalplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrentalplatform.mapper.CarRentalShopMapper;
import com.carrentalplatform.mapper.VehicleMapper;
import com.carrentalplatform.model.CarRentalShop;
import com.carrentalplatform.model.Vehicle;
import com.carrentalplatform.service.CarRentalShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

/**
 * 车行服务实现类
 */
@Service
public class CarRentalShopServiceImpl extends ServiceImpl<CarRentalShopMapper, CarRentalShop> implements CarRentalShopService {

    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public IPage<CarRentalShop> pageShops(int page, int size, String shopName) {
        Page<CarRentalShop> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<CarRentalShop> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果有车行名称，则模糊查询
        if (StringUtils.hasText(shopName)) {
            queryWrapper.like(CarRentalShop::getShopName, shopName);
        }
        
        // 只查询正常状态的车行
        queryWrapper.eq(CarRentalShop::getStatus, 1);
        
        // 按评分降序排序
        queryWrapper.orderByDesc(CarRentalShop::getShopScore);
        
        return this.page(pageParam, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateShopStatus(Long id, Integer status) {
        CarRentalShop shop = this.getById(id);
        if (shop == null) {
            return false;
        }
        
        shop.setStatus(status);
        return this.updateById(shop);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateShopScore(Long id, Double score) {
        CarRentalShop shop = this.getById(id);
        if (shop == null) {
            return false;
        }
        
        shop.setShopScore(new BigDecimal(score));
        return this.updateById(shop);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCarCount(Long id) {
        CarRentalShop shop = this.getById(id);
        if (shop == null) {
            return false;
        }
        
        // 统计该车行下的车辆总数
        LambdaQueryWrapper<Vehicle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Vehicle::getShopId, id);
        long totalCountLong = vehicleMapper.selectCount(queryWrapper);
        int totalCount = (int) totalCountLong;
        
        // 统计该车行下的可租车辆数量
        queryWrapper.clear();
        queryWrapper.eq(Vehicle::getShopId, id)
                   .eq(Vehicle::getStatus, 1);
        long availableCountLong = vehicleMapper.selectCount(queryWrapper);
        int availableCount = (int) availableCountLong;
        
        // 更新车行车辆数量
        shop.setTotalCars(totalCount);
        shop.setAvailableCars(availableCount);
        
        return this.updateById(shop);
    }
} 